<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>

<div class="container-fluid px-0">
<div class="row">
	<div class="col-12">
        <?php if(in_array($action,array('store','storehouse'))){ ?>
        <div class="row">
            <div class="hidden-sm col-md-8">
                <div class="btn-group mb-3" role="group">
                    <?php echo $plugin_cate_html;?>
                </div>
            </div>
            <?php if($action == 'store' && $data_verify !== FALSE){ ?>
            <div class="col-md-4 text-right">
                <div class="btn-group mb-3" role="group">
                    <a role="button" class="sync btn btn-secondary" href="javascript:void(0);"><?php echo lang('sync_official');?></a>
                </div>
            </div>
            <?php } ?>
        </div>
        <?php } ?>

		<div class="card">
			<div class="card-body">
				<div class="table-responsive">
					<?php if ($action == 'store' && $data_verify === FALSE) { ?>

                    <div class="col-md-4 mx-auto">
                        <p class="text-danger text-center font-weight-bold">
                            <?php echo lang('plugin_login_tips');?>
                        </p>
                        <div class="ajax_modal_body">
                            <form action="<?php echo url('plugin-read');?>" method="post" id="form">
                                <div class="form-group input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text"><i class="icon icon-user icon-fw"></i></span>
                                    </div>
                                    <input type="text" class="form-control" placeholder="<?php echo lang('email');?> / <?php echo lang('username');?>" id="email" name="email">
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text"><i class="icon icon-lock icon-fw"></i></span>
                                    </div>
                                    <input type="password" class="form-control" placeholder="<?php echo lang('password');?>" id="password" name="password">
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-primary btn-block" id="submit" data-loading-text="<?php echo lang('submiting');?>...">
                                        <?php echo lang('login');?>
                                    </button>
                                </div>
                                <div class="media">
                                    <div class="media-body text-right">
                                        <a target="_blank" href="http://www.wellcms.cn/user-create.html" class="text-muted">
                                            <small>
                                                <?php echo lang('user_create');?>
                                            </small>
                                        </a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

					<?php } else { ?>
					<table class="table table-hover table-striped">
						<?php if ($pluginlist) { ?>
						<?php foreach($pluginlist as $dir=>$plugin) { ?>

						<?php $plugin['name'] = array_value($plugin, 'name');
							$plugin['brief'] = array_value($plugin, 'brief');
							$plugin['version_fmt'] = $action == 'local' ? $plugin['version'] : array_value($plugin, 'official_version');
						?>

						<tr valign="top" dir="<?php echo $dir; ?>">
							<td width="80" class="text-center">
								<a href="<?php echo url("plugin-read-$dir");?>">
									<img src="<?php echo $plugin['icon_url']; ?>" width="54" height="54" />
								</a>
							</td>
							<td width="300">
								<a href="<?php echo url("plugin-read-$dir");?>">
                                <b>
                                    <?php echo $plugin['name']; ?>
                                </b>
                                </a>

								<span class="small mx-1">v<?php echo $plugin['version_fmt']; ?> </span>

								<?php if($plugin['have_upgrade']) { ?>
								<span class="small text-danger font-weight-bold mx-1">v<?php echo array_value($plugin, 'official_version'); ?> </span>
								<?php } ?>

								<br />
                                <span class="small text-muted">
                                    <?php echo $dir; ?>
                                </span>

								<?php if(!empty($plugin['username'])) { ?>
								<br />
                                <span class="small text-muted"><?php echo lang('author'); ?>：
								<a href="http://www.wellcms.cn/user-<?php echo $plugin['uid'];?>.htm" target="_blank"><?php echo $plugin['username'];?></a>
                                </span>
								<?php } ?>

							</td>
							<td width="100">
								<?php $price = $plugin['official']['price']; ?>
								<i class="icon-rmb"></i>
								<?php echo $price; ?>元
							</td>
							<td>
								<p class="grey"><?php echo $plugin['brief']; ?></p>
							</td>
							<td width="250" align="right">

                                <!-- 付费查看详情 -->
								<?php if($action == 'official_fee' && !$plugin['downloaded']) { ?>
								<a role="button" class="btn btn-primary btn-sm buy mx-1" href="<?php echo url("plugin-read-$dir"); ?>"><?php echo lang('buy');?></a>
								<?php } ?>

                                <!-- 免费按钮 -->
								<?php if($action == 'official_free' && !$plugin['downloaded']) { ?>
								<a role="button" class="btn btn-primary btn-sm download mx-1" href="<?php echo url("plugin-download-$dir"); ?>"><?php echo lang('download');?></a>
								<?php } ?>

								<?php if($plugin['setting_url']) { ?>
								<a role="button" class="btn btn-primary btn-sm setting mx-1" href="<?php echo url("plugin-setting-$dir"); ?>"><?php echo lang('setting');?></a>
								<?php } ?>

								<?php if(!$plugin['installed'] && $plugin['downloaded']) { ?>
								<a role="button" class="btn btn-primary btn-sm mx-1" href="<?php echo url("plugin-install-$dir", array('safe_token' => $safe_token)); ?>"><?php echo lang('install');?></a>
								<?php } ?>

								<?php if($plugin['installed'] && $plugin['enable']) { ?>
								<a id="disable" role="button" class="btn btn-secondary btn-sm disable mx-1" href="<?php echo url("plugin-disable-$dir", array('safe_token' => $safe_token)); ?>"><?php echo lang('disable');?></a>
								<?php } ?>

								<?php if($plugin['installed'] && !$plugin['enable']) { ?>
								<a id="enable" role="button" class="btn btn-secondary btn-sm enable mx-1" href="<?php echo url("plugin-enable-$dir", array('safe_token' => $safe_token)); ?>"><?php echo lang('enable');?></a>
								<?php } ?>

								<?php if($plugin['installed']) { ?>
								<a role="button" class="btn btn-danger btn-sm confirm mx-1" data-confirm-text="<?php echo lang('plugin_uninstall_confirm_tips', array('name'=>$plugin['name']));?>" href="<?php echo url("plugin-uninstall-$dir", array('safe_token' => $safe_token)); ?>"><?php echo lang('uninstall');?></a>
								<?php } ?>

								<?php if($plugin['have_upgrade']) { ?>
								<a role="button" class="btn btn-primary btn-sm upgrade mx-1" href="<?php echo url("plugin-upgrade-$dir"); ?>"><?php echo lang('update');?></a>
								<?php } ?>

							</td>
						</tr>
						<?php } ?>
						<?php } else { ?>
						<tr>
                            <td class="text-center">
                                <?php echo lang('none');?>
                            </td>
                        </tr>
						<?php } ?>
					</table>
                    <?php } ?>
				</div>
			</div>
		</div>

		<?php if($pagination) { ?> <nav><ul class="pagination justify-content-center"><?php echo $pagination; ?></ul></nav> <?php } ?>

	</div>

</div>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
    var safe_token = '<?php echo $safe_token;?>';
    <?php if ($action == 'storehouse') { ?>
        $('a.sync').on('click', function () {
            $.xpost(xn.url('plugin-storehouse'), {safe_token: safe_token}, function (code, message) {
                if (code == 0) {
                    $.alert(message).delay(1000).location();
                } else {
                    $.alert(message);
                }
            });
        });

        <?php if ($data_verify === FALSE) { ?>
            var jform = $('#form');
            jform.on('submit', function() {
                jform.reset();
                var jthis = $(this);
                var jsubmit = jthis.find('#submit');
                jsubmit.button('loading');
                var postdata = jform.serializeObject();
                $.xpost(jform.attr('action'), postdata, function(code, message) {
                    if(code == 0) {
                        jsubmit.button(message).delay(1000).location();
                    } else if(xn.is_number(code)) {
                        $.alert(message);
                        jsubmit.button('reset');
                    } else {
                        jform.find('[name="'+code+'"]').alert(message).focus();
                        jsubmit.button('reset');
                    }
                });
                return false;
            });
        <?php } ?>

    <?php } ?>

$('#plugin').addClass('show');
$('.sidebar-sticky li.menu-<?php echo $action;?>').addClass('active').prepend('<div class="arrow-tips float-right d-none d-md-block"></div>');
</script>